package com.fooddiary.service.impl;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.stereotype.Service;

import com.fooddiary.dao.IBaseDao;
import com.fooddiary.model.Model;
import com.fooddiary.util.AppException;
import com.fooddiary.util.ApplicationUtils;

@Service
public abstract class AbstractService {

	protected final Logger log = Logger.getLogger(getClass());
	
	@Autowired 
	protected IBaseDao baseDao;
	
	@Autowired
	protected ApplicationUtils applicationUtils;
	
	@Autowired
	protected MessageSource messageSource;
	
	/**
	 * Check model. If it equal to null then throws exception <br>
	 * @param p_model - model; {@link IModel} <br>
	 * @throws NoSuchMessageException - if no messages were founded in message.properties file <br>
	 * @throws AppException
	 */
	protected void checkModel(Model p_model) throws NoSuchMessageException, AppException{
		if(p_model == null){
			log.error(messageSource.getMessage("log.error.model.null", new Object[]{p_model}, null));
			throw new AppException( messageSource.getMessage( "message.error.common", null, null ) );
		}
	}
}
